home *** CD-ROM | disk | FTP | other *** search
- Path: anvil.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c++,comp.lang.c
- Subject: Re: Q: follow(expect,ifyes,ifno) buggy?
- Date: 28 Feb 1996 08:50:55 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4h215fINNmoi@anvil.ugrad.cs.ubc.ca>
- References: <4gv9a9$nfu@tuegate.tue.nl>
- NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
-
- In article <4gv9a9$nfu@tuegate.tue.nl>,
- Pieter Kuppens <pwk@news.eb.ele.tue.nl> wrote:
- >In compiler construction (with C) the following function is often used:
- >
- >follow(expect,ifyes,ifno)
- >{
- > int c = getchar();
- > if (c==expect) return ifyes;
- > ungetc(c,stdin);
- > return ifno;
- >}
- >
- >I have my doubts on this function. If calls are nested:
- >(abbrev. LA (look ahead) instead of follow)
- >LA('/',LA('/',COMMENT_LINE,LA('*',COMMENT,DIV)))
- >may very well fail on the following:
- >nested call LA is evaluated in the function call, before another
- >function LA is completed. Look ahead at 1 position further can disturb
- >correct evaluation of this function.
-
- Does the LA macro insert a proper "ifno" return value for each call?
- You are invoking the outermost LA with only one argument, but the second and
- innermost one with three arguments.
-
- Please check your code for basic syntactic correctness before asking deeper
- questions related to the semantics.
-
- Are you not aware that the innermost nested function will be called first, thus
- you will be matching the '*' token before the outermost '/'? In C (and other
- languages similar to C), the arguments of a function must be evaluated before
- that function is called.
-
- In writing a predictive parser, what you typically want is recursion, not
- nested function calls! Else reverse the order, so that the slash is matched in
- the innermost call.
-
- By the way, it's not good practice to omit writing explict types in your
- function declaration/definition.
-
- >follow(expect,ifyes,ifno)
-
- Is there a specific reason why you can't use an ANSI prototype?
-
- Do you realize that your function is not even proper *old style* C? You have
- not declared expect, ifyes and ifno to have a type.
-
- >Questions:
- >(1) am I right in the expected problem?
- >(2) how can this problem be evaded
-
- Write a state machine that looks for comments. Comments can be parsed by a
- regular grammar---you don't need an LL(1) predictive parser to look for
- comments. A state machine, based on a regular expression that swallows comments
- is sufficient. You don't need a push-down automaton, just an FSM.
-
- >(3) is there an C++ equivalent of this function (without the problem)
-
- Learn C first, then C++. ;)
- --
-
-